<?php
declare(strict_types=1);
namespace App\Service\Topic\Topic;
use Auth;
use App\Models\Topic\Topic;
class TopicSave implements TopicSaveInterface {

    public function store($request,$topic):int{
        $topic->fill($request->all());
        $topic->user_id = Auth::id();
        $topic->save();
        return $topic->id;
    }
    public function save($request, $topic):bool{
        return true;
    }
    public function uploadImage($request,$uploader):array{
        // 初始化返回数据，默认是失败的
        $data = [
            'success'   => false,
            'msg'       => '上传失败!',
            'file_path' => ''
        ];
        // 判断是否有上传文件，并赋值给 $file
        if ($file = $request->upload_file) {
            // 保存图片到本地
            $result = $uploader->save($file, 'topics',\Auth::id(), 1024);
            // 图片保存成功的话
            if ($result) {
                $data['file_path'] = $result['path'];
                $data['msg']       = "上传成功!";
                $data['success']   = true;
            }
        }
        return $data;
    }
    public function destroy($topic):bool{
        $topic->update(['status'=>Topic::StatusFalse]);
        return true;
    }
}
